<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Csyslog extends controller {
	
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$table  = C\model::load('main.sys.syslog')->set_ymd()->get_table();
		$dlang  = lang::inst('dd');
		$view->assign_by_ref('table', $table);
		$view->assign('type', $dlang->get('sh0004'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('core/syslog.ext.js');
	}
	
	/**
	 * 日志数据的获取
	 * Enter description here ...
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$user   = input::post('user');
		if ($user && !empty($user)) {
			$where[] = array('user', '%'.$user.'%', \classlib\orm::OP_LIKE);
		}
		$origin = input::post('origin');
		if ($origin && !empty($origin)) {
			$where[] = array('path', '%'.$origin.'%', \classlib\orm::OP_LIKE);
		}
		$msg   = input::post('msg');
		if ($msg && !empty($msg)) {
			$where[] = array('msg', '%'.$msg.'%', \classlib\orm::OP_LIKE);
		}
		$sdate = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$type  = input::post('type');
		if (is_numeric($type)) {
			$where['type'] = $type;
		}
		$table = input::get_post('table');
		if (empty($table)) {
			$table = C\model::load('main.sys.syslog')->set_ymd()->get_table();
		}
		$orm   = C\model::load('main.sys.syslog')->set_table($table);
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
				$item['msg']   = htmlspecialchars($item['msg']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}